/**
 * stage:
 * 0: perpare to pregnant
 * 10000: pregnanted
 * 20000: born
 *
 */

class Catalog {
  static mapping = {
    table 'yuying_catalog'
  }
  String name
  String description
  String imgUrl
  Integer displayPosition
  Integer stage
  Catalog parent
  Date dateCreated
  Date lastUpdated

  static constraints = {
    stage(nullable: false)
    displayPosition(nullable: true)
    imgUrl(nullable: true)
    parent(nullable: true)
    stage(nullable: true)
  }

  def getDepth() {
    def depth = this.id.toString()
    Catalog p = this.parent
    int d = 0
    while (p) {
      depth = p.id.toString() + "/" + depth
      p = p.parent
      if (d++ > 10) break;
    }
    return depth
  }

  def getSubCategories() {
    return Catalog.findAllByParent(this)
  }

  public int compareTo(Object o) {
    Catalog obj = (Catalog) o;
    if (this.displayPosition == null || this.displayPosition.equals(obj.displayPosition)) {
      return this.dateCreated.compareTo(obj.dateCreated);
    }
    return displayPosition.compareTo(obj.displayPosition);
  }

  public String toString() {
    return "${id}:${name}:${description}"
  }
}
